package com.wedistudio.slides.search;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.wedistudio.slides.search.slideshare.SlideShareSource;

public class Search {

	private String keywords;
	private int offset;
	private int limit;
	private OrderBy orderby;
	private Language language;

	public Search(String keywords, Language language, OrderBy orderby,
			int offset, int limit) {
		super();
		this.keywords = keywords;
		this.orderby = orderby;
		this.offset = offset;
		this.limit = limit;
		this.language = language;
	}

	public String getKeywords() {
		return keywords;
	}

	public int getOffset() {
		return offset;
	}

	public int getLimit() {
		return limit;
	}

	public OrderBy getOrderby() {
		return orderby;
	}

	public Language getLanguage() {
		return language;
	}

	private static List<Source> SOURCES;

	static {
		SOURCES = new LinkedList<Source>();
		SOURCES.add(new SlideShareSource());
	}

	public static List<ContentHeader> getContents(String keywords,
			Language language, OrderBy orderby, int offset, int limit) {
		Search criteria = new Search(keywords, language, orderby, offset, limit);
		List<ContentHeader> result = new ArrayList<ContentHeader>();
		for (Source src : SOURCES) {
			List<ContentHeader> subResult = src.getContents(criteria);
			if (subResult != null)
				result.addAll(subResult);
		}
		return result;
	}

	public static List<ContentHeader> getRelatedContents(ContentHeader content) {
		List<ContentHeader> result = new ArrayList<ContentHeader>();
		for (Source src : SOURCES) {
			List<ContentHeader> subResult = src.getRelatedContents(content);
			if (subResult != null)
				result.addAll(subResult);
		}
		return result;
	}

	public static List<ContentHeader> getContents(Author author) {
		List<ContentHeader> result = new ArrayList<ContentHeader>();
		for (Source src : SOURCES) {
			List<ContentHeader> subResult = src.getContents(author);
			if (subResult != null)
				result.addAll(subResult);
		}
		return result;
	}

	public static Content complete(ContentHeader content) {
		for (Source src : SOURCES) {
			if (src.getSource().equals(content.getSource()))
				return src.complete(content);
		}
		return null;
	}
}
